package com.jie.langlangmall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.common.constant.MessageConstant;
import com.jie.langlangmall.common.exception.ApiException;
import com.jie.langlangmall.dto.PmsBrandParam;
import com.jie.langlangmall.mapper.PmsBrandMapper;
import com.jie.langlangmall.mapper.PmsProductMapper;
import com.jie.langlangmall.mapper.SmsHomeBrandMapper;
import com.jie.langlangmall.model.*;
import com.jie.langlangmall.service.PmsBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/23
 * @Email 3217998214@qq.com
 * @Description 商品品牌管理
 */
@Service
@Slf4j
public class PmsBrandServiceImpl implements PmsBrandService {

    @Autowired
    private PmsBrandMapper brandMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private SmsHomeBrandMapper homeBrandMapper;

    /**
     * 商品品牌分页查询
     *
     * @param keyWord
     * @param showStatus
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<PmsBrand> list(String keyWord, Integer showStatus, Integer pageSize, Integer pageNum) {
        // 1. 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        // 2. 构建查询参数
        PmsBrandExample example = new PmsBrandExample();
        example.setOrderByClause("sort desc");
        PmsBrandExample.Criteria criteria = example.createCriteria();
        // 品牌名字
        if (StrUtil.isNotEmpty(keyWord)) {
            criteria.andNameLike("%" + keyWord + "%");
        }
        // 显示状态
        if (showStatus != null) {
            criteria.andShowStatusEqualTo(showStatus);
        }
        // 3. 执行查询
        return brandMapper.selectByExample(example);
    }

    /**
     * 查询所有的品牌列表
     *
     * @return
     */
    @Override
    public List<PmsBrand> listAll() {
        return brandMapper.selectByExample(new PmsBrandExample());
    }

    /**
     * 添加新的品牌
     *
     * @param brandParam
     */
    @Override
    public void create(PmsBrandParam brandParam) {
        PmsBrand brand = BeanUtil.copyProperties(brandParam, PmsBrand.class);
        // 如果创建的时候，首字母是空的，就取第一个字母为首字母
        if (StrUtil.isEmpty(brand.getFirstLetter())) {
            brand.setFirstLetter(brand.getName().substring(0, 1));
        }
        brandMapper.insertSelective(brand);
    }

    /**
     * 更新品牌
     *
     * @param id
     * @param brandParam
     */
    @Override
    public void update(Long id, PmsBrandParam brandParam) {
        PmsBrand brand = BeanUtil.copyProperties(brandParam, PmsBrand.class);
        brand.setId(id);
        // 1. 如果更新的时候，首字母是空的，就取第一个字母为首字母
        if (StrUtil.isEmpty(brand.getFirstLetter())) {
            brand.setFirstLetter(brand.getName().substring(0, 1));
        }
        // TODO: 这是一个很耗时的操作，怎么优化？
        // 2. 更新品牌的时候，需要更新商品中对应的品牌的名称 update from pms_product set brand_name = ? where brand_id = ?
        PmsProductExample productExample = new PmsProductExample();
        productExample.createCriteria().andBrandIdEqualTo(id);
        PmsProduct product = new PmsProduct();
        product.setBrandName(brand.getName());
        productMapper.updateByExampleSelective(product, productExample);

        // 3. 更新品牌
        brandMapper.updateByPrimaryKeySelective(brand);

        // 4. 更新推荐品牌表
        SmsHomeBrandExample homeBrandExample = new SmsHomeBrandExample();
        homeBrandExample.createCriteria().andBrandIdEqualTo(id);
        SmsHomeBrand homeBrand = new SmsHomeBrand();
        homeBrand.setBrandName(brand.getName());
        homeBrandMapper.updateByExampleSelective(homeBrand, homeBrandExample);
    }

    /**
     * 删除品牌
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        // 1. 如果品牌下还有商品，不能删除
        PmsProductExample productExample = new PmsProductExample();
        productExample.createCriteria().andBrandIdEqualTo(id).andDeleteStatusEqualTo(0);
        long count = productMapper.countByExample(productExample);
        if (count > 0) {
            throw new ApiException(MessageConstant.BRAND_CONNECT_PRODUCT);
        }

        // 2. 如果是推荐品牌，不能删除
        SmsHomeBrandExample homeBrandExample = new SmsHomeBrandExample();
        homeBrandExample.createCriteria().andBrandIdEqualTo(id);
        count = homeBrandMapper.countByExample(homeBrandExample);
        if (count > 0) {
            throw new ApiException(MessageConstant.BRAND_RECOMMEND);
        }

        // 3. 删除品牌
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        // 1. 如果品牌下面还有商品，不能删除
        PmsProductExample productExample = new PmsProductExample();
        productExample.createCriteria().andBrandIdIn(ids).andDeleteStatusEqualTo(0);
        long count = productMapper.countByExample(productExample);
        if (count > 0) {
            throw new ApiException(MessageConstant.BRAND_CONNECT_PRODUCT);
        }

        // 2. 如果是推荐品牌，不能删除
        SmsHomeBrandExample homeBrandExample = new SmsHomeBrandExample();
        homeBrandExample.createCriteria().andBrandIdIn(ids);
        count = homeBrandMapper.countByExample(homeBrandExample);
        if (count > 0) {
            throw new ApiException(MessageConstant.BRAND_RECOMMEND);
        }

        // 3. 删除品牌
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        brandMapper.deleteByExample(pmsBrandExample);
    }

    /**
     * 批量更新显示状态
     *
     * @param ids
     * @param showStatus
     */
    @Override
    public void updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsBrand brand = new PmsBrand();
        brand.setShowStatus(showStatus);
        PmsBrandExample example = new PmsBrandExample();
        example.createCriteria().andIdIn(ids);
        brandMapper.updateByExampleSelective(brand, example);
    }

    /**
     * 批量更新厂家制造商状态
     *
     * @param ids
     * @param factoryStatus
     */
    @Override
    public void updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        PmsBrand brand = new PmsBrand();
        brand.setFactoryStatus(factoryStatus);
        PmsBrandExample example = new PmsBrandExample();
        example.createCriteria().andIdIn(ids);
        brandMapper.updateByExampleSelective(brand, example);
    }

    /**
     * 根据ID获取品牌信息
     *
     * @param id
     * @return
     */
    @Override
    public PmsBrand getItem(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

}
